package com.kly.module_base.dialog

import androidx.fragment.app.FragmentManager
import android.util.SparseArray
import android.view.View
import com.kly.module_base.dialog.help.DismissListener
import com.kly.module_base.dialog.help.Contorller
import com.kly.module_base.dialog.help.LogicListener

class CommonDialog : CommonBaseDialog() {

    private var c: Contorller? = null

    init {
        c = Contorller()
    }

    fun getDialogView(): View? {
        return view
    }

    override fun getFullScreen(): Boolean {
        return c?.getFullScreen() ?: false
    }

    override fun getLayout(): Int {
        return c?.getLayoutId()!!
    }

    override fun getContentView(): View? {
        return c?.getContentView()
    }

    override fun getLogiclistener(): LogicListener? {
        return c?.getLogiclistener()
    }

    override fun dismissCallback(): DismissListener? {
        return c?.dismissCallback()
    }

    override fun getDialogOutTransparency(): Float {
        return c?.getDialogOutTransparency()!!
    }

    override fun getDialogCancel(): Boolean {
        return c?.getDialogCancel()!!
    }

    override fun getViews(): SparseArray<Int>? {
        return c?.getViews()
    }

    override fun getAnimation(): Int? {
        return c?.getAnimation()
    }


    override fun getClickListener(): View.OnClickListener? {
        return c?.getClickListener()
    }

    override fun getGravity(): Int? {
        return c?.getGravity()
    }

    class Builder {
        private var p: Contorller.Params? = null
        private var d: CommonDialog? = null

        init {
            p = Contorller.Params()
        }

        fun show(vararg tag: String): Builder {
            d = CommonDialog()
            p?.apply(d!!.c)
            val fragmentManager = d!!.c?.getFragmentManager()
            if (tag.isNotEmpty()) {
                d!!.show(fragmentManager!!, tag[0])
            } else {
                d!!.show(fragmentManager!!, "sd")
            }
            return this
        }

        fun dismiss() {
            d!!.dismissAllowingStateLoss()
        }

        fun isShow(): Boolean {
            var isShow = false
            isShow = (d!!.dialog != null)
            if (isShow) {
                isShow = d!!.dialog!!.isShowing
            }
            return isShow
        }


        /**
         * 设置FragmentManager管理器
         * @param fragmentManager
         * */
        fun setFramentManager(fragmentManager: FragmentManager): Builder {
            p?.k_fragmentManager = fragmentManager
            return this
        }

        /**
         * 设置Layout
         * @param layoutId 自定义布局
         * */
        fun setContentView(layoutId: Int): Builder {
            p?.k_layoutId = layoutId
            return this
        }

        /**
         * 设置contentView
         * @param contentView 指定View
         * */
        fun setContentView(contentView: View): Builder {
            p?.k_contentView = contentView
            return this
        }

        /**
         * 设置Dialog弹出框动画
         *@param styleAnimation 可以指直接指定一个style 默认没有动画
         * */
        fun setAnimation(styleAnimation: Int): Builder {
            p?.k_dialogAnimation = styleAnimation
            return this
        }

        /**
         * 屏蔽触摸dialog框以外屏幕、返回键  关闭dialog
         * @param cancelable false 不屏蔽
         * */
        fun setCancelable(cancelable: Boolean): Builder {
            p?.k_cancelable = cancelable
            return this
        }

        /**
         * 设置Dialog位置
         * @param Gravity
         * */
        fun setGravity(gravity: Int): Builder {
            p?.k_dialogGravity = gravity
            return this
        }

        /**
         * 设置Dialog 是否全屏幕
         * @param Gravity
         * */
        fun setFullScreen(fullScreen: Boolean): Builder {
            p?.fullScreen = fullScreen
            return this
        }

        /**
         * 设置Dialog框以外的背景透明度
         * 默认0.5f
         * @param transparency 0 - 1f
         * */
        fun setDialogOutTransparency(transparency: Float): Builder {
            p?.k_dialogOutTransparency = transparency
            return this
        }

        /**
         * 批量设置点击事件
         * @param viewId 可变参数
         * */
        fun setViewClick(listener: View.OnClickListener, vararg viewId: Int): Builder {
            p?.k_listener = listener
            for (it in viewId) {
                p?.mViews?.put(it, it)
            }
            return this
        }

        /**
         * dialog dismiss 回调
         * @param
         * */
        fun addDismissListener(listener: DismissListener): Builder {
            p?.k_dismisslistener = listener
            return this
        }

        /**
         * 业务逻辑回调,这里可用于dialog显示后的业务处理
         * @param
         * */
        fun addLogicListener(listener: LogicListener): Builder {
            p?.k_LogicListener = listener
            return this
        }
    }
}